package com.water.camera.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.plata.base.aclr.utils.SPHelper;
import com.water.camera.R;
import com.water.camera.databinding.DialogDmWatermarkBinding;
import com.water.camera.it.WaterMarkChangeListener;
import com.water.camera.it.WaterMarkViewChangeListener;
import com.water.camera.mark.BaseMark;
import com.water.camera.util.ToastUtil;

public class MarkDlg extends FullScreenDlg {

    DialogDmWatermarkBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DialogDmWatermarkBinding.inflate(inflater, container, false);

        replaceFragment(R.id.container, new MarksFragment().setWaterMarkChangeListener(new WaterMarkChangeListener() {
            @Override
            public void onWaterMarkChange(String name) {
                try {
                    BaseMark view = (BaseMark) Class.forName(name).getDeclaredConstructor(Context.class, AttributeSet.class)
                            .newInstance(getActivity(), null);
                    if (getActivity() instanceof WaterMarkViewChangeListener) {
                        if (((WaterMarkViewChangeListener) getActivity()).needScale()) {
                            String key = view.getClass().getSimpleName() + "_scale";
                            float scale = SPHelper.getInstance(getContext()).getFloat(key, 1.0f);
                            view.setScale(scale);
                        }
                        ((WaterMarkViewChangeListener) getActivity()).onWaterMarkViewChange(view);
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                dismiss();
            }
        }));
        binding.getRoot().setOnClickListener(v -> dismiss());
        binding.cleanWaterMaskBtn.setOnClickListener(v -> {
            if (getActivity() instanceof WaterMarkViewChangeListener) {
                ((WaterMarkViewChangeListener) getActivity()).onWaterMarkViewChange(null);
                ToastUtil.showShortToast(getActivity(), "关闭水印");
            }
            dismiss();
        });

        return binding.getRoot();
    }
}
